﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace VirtualOperator
{
    public enum Headers {INVITE, BYE, CANCEL, RINGING, TRYING, OK}
    public enum UniType {SWITCHED, HARD, SOFT, NONE}
    public class CallerConnection
    {
        private static Random rand = new Random();
        public static Direction Direction;
        public string SourceName{get; private set;}
        public string TargetName { get; private set; }

        public int SourceId { get; private set; }
        public int TargetId { get; private set; }

        public int Capacity { get; private set; }
        public UniType UniType { get; private set; }

        private bool InviteFlag;
        private bool TryingFlag;
        private bool RingingFlag;
        private bool OkFlag;

        public bool Connected{get {return InviteFlag && TryingFlag && RingingFlag && OkFlag;}}
        public int ConnectionRand { get; private set; }
        public int CallId{ get; set; }

        public CallerConnection(string sourceName, int sourceId, string targetName,int targetId, int capacity, UniType uni)
        {
            ConnectionRand = rand.Next() % 10000;
            SourceName =sourceName;
            TargetName = targetName;
            SourceId = sourceId;
            TargetId = targetId;
            Capacity = capacity;
            CallId =-1; //means not connected
            this.UniType = uni;
        }

        public CallerConnection()
        {
           
        }


        public string ToInvite()
        {
            InviteFlag = true;
            this.UniType = UniType.SWITCHED;
            return String.Format("{0} {1} {2} {3} {4} {5} {6} {7}", Headers.INVITE, ConnectionRand, CallId, SourceId, SourceName, TargetId, TargetName, Capacity);
        }

        public string ToOK()
        {
            OkFlag = true;
            return String.Format("{0} {1} {2}", Headers.OK, ConnectionRand, CallId);
        
        }

        public string ToTrying()
        {
            TryingFlag = true;
            return String.Format("{0} {1}", Headers.TRYING, ConnectionRand);
        }


        public string ToRinging()
        {
            RingingFlag = true;
            return String.Format("{0} {1}", Headers.RINGING, ConnectionRand);
        }

        public string ToCancel()
        {
            RingingFlag = false;
            OkFlag = false;
            TryingFlag = false;

            return String.Format("{0} {1}", Headers.CANCEL, ConnectionRand);
        }


        public string ToBye()
        {
            RingingFlag = false;
            OkFlag = false;
            TryingFlag = false;
            return String.Format("{0} {1}", Headers.BYE, ConnectionRand);
        }

        public bool ParseInvite(string [] query)
        {
            if (query.Length==8 && Headers.INVITE.ToString() == query[0])
            { 
                CallId = Int32.Parse(query[2]);
                ConnectionRand = Int32.Parse(query[1]);
                SourceId = Int32.Parse(query[3]);
                SourceName = query[4];
                TargetId = Int32.Parse(query[5]);
                TargetName = query[6];
                Capacity = Int32.Parse(query[7]);
                InviteFlag = true;
            }
            return InviteFlag;
        }

        public bool ParseTrying(string[] query)
        {
            if (query.Length == 2 && Headers.TRYING.ToString() == query[0])
            {
                TryingFlag = true;
            }
            return TryingFlag;
        }

        public bool ParseRinging(string[] query)
        {
            if (query.Length == 2 && Headers.RINGING.ToString() == query[0])
                RingingFlag = true;

            return RingingFlag;
        }


        public bool ParseOK(string[] query)
        {
            if (query.Length == 3 && Headers.OK.ToString() == query[0])
            {
                OkFlag = true;
                CallId = Int32.Parse(query[2]);
            }
            return OkFlag;
        
        }


        public void FillMissingSpots(int id, string name)
        {
            TargetId = id;
            TargetName = name;
            
        }


        public override string ToString()
        {
            return SourceName +"->"+TargetName;
        }
    }



}
